package com.cn.controller;

import com.cn.vo.PostUserEntityReqVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: springcloud-example
 * @description:
 * @author:
 * @create: 2018-06-15 15:55
 **/
@RestController
public class HystrixController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getDefaultNullUser")
    @GetMapping("/getNullUser")
    @ResponseBody
    public String getNullUser() {
        return restTemplate.getForObject("http://client-87/getNullUser", String.class);
    }

    private String getDefaultNullUser() {
        return "getNullUser" + "default Name";
    }

    @HystrixCommand(fallbackMethod = "getDefaultUser")
    @GetMapping("/getUser")
    @ResponseBody
    public String getUser(@RequestParam String name) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        return restTemplate.getForObject("http://client-87/getUser?name={name}", String.class, map);
    }

    private String getDefaultUser(String name) {
        return "getUser" + "default Name : " + name;
    }

    @HystrixCommand(fallbackMethod = "postDefaultNullUser")
    @PostMapping("/postNullUser")
    @ResponseBody
    public String postNullUser() {
        return restTemplate.postForObject("http://client-87/postNullUser", null, String.class);
    }

    private String postDefaultNullUser() {
        return "postNullUser" + "default Name";
    }

    @HystrixCommand(fallbackMethod = "postDefaultUser")
    @PostMapping("/postUser")
    @ResponseBody
    public String postUser(@RequestBody String name) {
        return restTemplate.postForObject("http://client-87/postUser", name, String.class);
    }

    private String postDefaultUser(@RequestBody String name) {
        return "postUser" + "default Name : " + name;
    }

    @HystrixCommand(fallbackMethod = "postDefaultUserEntity")
    @PostMapping("/postUserEntity")
    @ResponseBody
    public String postUserEntity(@RequestBody PostUserEntityReqVO reqVO) {
        return restTemplate.postForObject("http://client-87/postUserEntity", reqVO, String.class);
    }

    private String postDefaultUserEntity(@RequestBody PostUserEntityReqVO reqVO) {
        return "postUserEntity" + "default Name : " + reqVO.getName();
    }

    @GetMapping("/loadInstance")
    public String loadInstance() {
        ServiceInstance choose = this.loadBalancerClient.choose("client-87");
        System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort());
        return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort();
    }
}
